package com.lagado.rrex;

import java.util.Formatter;

public class Alternation {

    RRex precedingPattern;
    Formatter fmt = new Formatter();

    Alternation(RRex precedingPattern) {
        this.precedingPattern = precedingPattern;
    }

    public RRex Either(RRex firstOption, RRex secondOption) {
        return precedingPattern.addFormatted(
                "(%1$s|%2$s)", firstOption, secondOption);
    }

    public RRex If(RRex matched, RRex then, RRex otherwise) {
        return precedingPattern.addFormatted(
                "(?(?=%1$s)%2$s|%3$s)", matched, then, otherwise);
    }

    public RRex If(String namedGroupToMatch, RRex then, RRex otherwise) {
        return precedingPattern.addFormatted(
                "(?(%1$s)%2$s|%3$s)", namedGroupToMatch, then, otherwise);
    }

    public RRex If(int unnamedCaptureToMatch, RRex then, RRex otherwise) {
        return precedingPattern.addFormatted(
                "(?(%1$s)%2$s|%3$s)", unnamedCaptureToMatch, then, otherwise);
    }
}
